<template>
  <div
    class="play"
    :style="{ transform: `rotate(${isShowPlayPage ? '0deg' : '90deg'})` }"
  >
    <div
      class="control-bg"
      :style="{ backgroundImage: `url(${picUrl})` }"
    ></div>

    <!-- 导航栏 -->
    <div class="head">
      <!-- 关闭 -->
      <div class="left" @click="closePlayPage">
        <van-icon name="arrow-down" />
      </div>

      <!-- 名字 -->
      <div class="center">
        <div class="song">{{ songName }}</div>
        <div class="songer">
          <div class="songerbox" @click="viewSonger(singerId)">
            {{ songerName }}
          </div>
          <div class="follow">+</div>
        </div>
      </div>

      <!-- 分享 -->
      <div class="right" @click="showShare = true">
        <van-icon name="music-o" />
      </div>
    </div>

    <!-- 转盘 -->
    <div
      class="disc rotate"
      v-if="picUrl !== ''"
      :style="{ animationPlayState: isPaused ? 'paused' : 'running' }"
      @click="golyricshow"
      v-show="discshow"
    >
      <img :src="picUrl" alt="" />
    </div>

    <div class="disc" v-else @click="golyricshow" v-show="discshow">
      <img src="../assets/logo.svg" alt="" />
    </div>

    <!-- 歌词 -->
    <div class="lyric" v-show="lyricshow" @click="godiscshow">
      <div class="lyricbox" :style="{ top: top + 'px' }">
        <div
          class="item"
          ref="lyric"
          v-for="(item, index) in lyric"
          :key="index"
        >
          {{ item.text }}
        </div>
      </div>
    </div>

    <!-- 功能 -->
    <div class="function">
      <div class="functionlist">
        <div class="functionbox" @click="addLike">
          <img :src="likeshow ? likeIcon : nolikeIcon" alt="" />
        </div>
        <div class="functionbox">
          <img src="../assets/play/download.svg" alt="" />
        </div>
        <div class="functionbox">
          <img src="../assets/play/sing.svg" alt="" />
        </div>
        <div class="functionbox" @click="commentshow = true">
          <img src="../assets/play/text.svg" alt="" />
        </div>
        <div class="functionbox">
          <img src="../assets/play/menu.svg" alt="" />
        </div>
      </div>
    </div>

    <!-- 进度条 -->
    <div class="bar">
      <div class="timeleft">{{ currenttime }}</div>

      <!-- 滑块 -->
      <div class="slider">
        <van-slider v-model="value" bar-height="4px" active-color="#ee0a24" />
      </div>

      <div class="timeright">{{ durationtime }}</div>
    </div>

    <!-- 播放 -->
    <div class="playtion">
      <div class="playlist">
        <div class="playbox">
          <img src="../assets/play/loop.svg" alt="" />
        </div>
        <div class="playbox">
          <img src="../assets/play/left.svg" alt="" />
        </div>
        <div class="playbox" @click="playAndStop">
          <img :src="isPaused ? playIcon : pauseIcon" alt="" />
        </div>
        <div class="playbox">
          <img src="../assets/play/right.svg" alt="" />
        </div>
        <div class="playbox" @click="show = true">
          <img src="../assets/play/list.svg" alt="" />
        </div>
      </div>
    </div>

    <!-- 评论列表 -->
    <div class="comment">
      <van-popup
        v-model="commentshow"
        round
        position="bottom"
        :style="{ height: '80%' }"
      >
        <div class="commenttitle">精彩评论</div>

        <div class="commentmain">
          <div
            class="commentlist"
            v-for="(item, index) in audioComment"
            :key="index"
          >
            <div class="listhead">
              <van-image
                width="50"
                height="50"
                round
                lazy-load
                :src="item.user.avatarUrl"
              >
                <template v-slot:loading>
                  <van-loading type="spinner" size="20" />
                </template>
              </van-image>
            </div>

            <div class="listtext">
              <div class="listtextbox">
                <div class="txtbox">
                  <div class="user">{{ item.user.nickname }}</div>
                  <div class="commenttime">{{ item.time | formatDate }}</div>
                </div>

                <div class="like">
                  <div class="liketxt">{{ item.likedCount }}</div>
                  <van-icon name="flower-o" />
                </div>
              </div>

              <div class="commenttext">{{ item.content }}</div>
            </div>
          </div>
        </div>
      </van-popup>
    </div>

    <!-- 播放歌曲列表 -->
    <div class="songlist">
      <van-popup v-model="show">
        <div class="playtit">播放列表</div>

        <div v-if="defaultSongList.length === 0 ? true : false">
          <van-empty description="列表空空，快去听歌吧" />
        </div>

        <!-- 歌曲列表 -->
        <div
          class="tabbox"
          v-else
          v-for="(item, index) in defaultSongList"
          :key="index"
          @click="
            playAudio(
              item.songId,
              item.picUrl,
              item.songName,
              item.singerName,
              item.singerId
            )
          "
        >
          <!-- 封图 -->
          <div class="sealing">
            <van-image :src="item.picUrl" lazy-load>
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
            </van-image>
          </div>

          <!-- 名字 -->
          <div class="tabname">
            <div class="tabsongname">{{ item.songName }}</div>
            <div class="tabtxt">
              <div class="textbox">SQ</div>
              {{ item.singerName }}
            </div>
          </div>

          <div class="tabplay">
            <van-icon
              :name="
                audioId !== item.songId
                  ? 'play-circle-o'
                  : isPaused
                  ? 'play-circle-o'
                  : 'pause-circle-o'
              "
            />
          </div>

          <div class="share" @click="showShare = true">
            <van-icon name="ellipsis" />
          </div>
        </div>
      </van-popup>
    </div>

    <!-- 分享面版 -->
    <van-share-sheet
      v-model="showShare"
      title="立即分享给好友"
      :options="options"
    />
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "Play",

  props: {
    //当前音频播放时间
    currentTime: {
      type: Number,
      default: 0,
    },

    //歌词
    lyric: {
      type: Array,
      default() {
        return [];
      },
    },
  },

  watch: {
    currentTime(newvalue) {
      //根据当前音频播放时间匹配歌词
      //当前音频播放时间 currentTime
      //前一句歌词的时间 beforeTime
      //后一句歌词的时间 afterTime
      //判断是否显示前一句歌词
      //currentTime >= beforeTime && currentTime <= afterTime
      for (let i = 0; i < this.lyric.length; i++) {
        let afterTime = this.lyric[i + 1].time;

        //获取音频总时间
        //获取分钟
        let minutes = Math.floor((this.audioEle.duration - newvalue) / 60);
        minutes = minutes >= 10 ? minutes : "0" + minutes;

        //获取秒钟
        let seconds = Math.floor((this.audioEle.duration - newvalue) % 60);
        seconds = seconds >= 10 ? seconds : "0" + seconds;

        this.durationtime = `${minutes}:${seconds}`;

        //获取音频的当前播放时间
        //获取分钟
        let min = Math.floor(newvalue / 60);
        min = min >= 10 ? min : "0" + min;

        //获取秒钟
        let sec = Math.floor(newvalue % 60);
        sec = sec >= 10 ? sec : "0" + sec;

        this.currenttime = `${min}:${sec}`;

        //进度条
        this.value = (newvalue / this.audioEle.duration) * 100;
        this.changeStateProperty({
          key: "rate",
          value: (newvalue / this.audioEle.duration) * 100,
        });

        //防止i越界
        if (i + 1 === this.lyric.length) {
          afterTime = Number.MAX_VALUE;
        }

        if (newvalue >= this.lyric[i].time && newvalue < afterTime) {
          //获取当前播放的歌词
          if (this.currentIndex === i) {
            return;
          }

          //获取第 i 个div.item的高度，设置top
          this.top -= 30;
          this.currentIndex = i;
          this.$refs.lyric[i - 1].classList.remove("active");
          this.$refs.lyric[i].classList.add("active");
          break;
        }

        if (this.value == 100) {
          //暂停音频
          this.audioEle.pause();
          this.changeStateProperty({ key: "isPaused", value: true });
        }
      }
    },
  },

  data() {
    return {
      showShare: false,
      options: [
        [
          { name: "微信", icon: "wechat" },
          { name: "朋友圈", icon: "wechat-moments" },
          { name: "微博", icon: "weibo" },
          { name: "QQ", icon: "qq" },
        ],
        [
          { name: "复制链接", icon: "link" },
          { name: "分享海报", icon: "poster" },
          { name: "二维码", icon: "qrcode" },
          { name: "小程序码", icon: "weapp-qrcode" },
        ],
      ],

      playIcon: require("../assets/play/play.svg"),
      pauseIcon: require("../assets/play/stop.svg"),

      //进度条值
      value: 0,

      lyricshow: false,
      discshow: true,

      commentshow: false,

      show: false,

      //歌词移动的top
      top: 120,

      //当前的歌词下标
      currentIndex: 0,

      durationtime: "",
      currenttime: "",

      likeshow: false,
      likeIcon: require("../assets/play/love_.svg"),
      nolikeIcon: require("../assets/play/love.svg"),
    };
  },

  computed: {
    ...mapState([
      "isShowPlayPage",
      "picUrl",
      "isPaused",
      "audioEle",
      "songName",
      "songerName",
      "singerId",
      "audioComment",
      "defaultSongList",
      "audioId",
      "Likesong",
    ]),
  },

  methods: {
    //切换转盘、歌词
    golyricshow() {
      this.discshow = false;
      this.lyricshow = true;
    },

    godiscshow() {
      this.lyricshow = false;
      this.discshow = true;
    },

    //查看歌手页面
    viewSonger(id) {
      this.$router.push({ name: "Songer", params: { id } });
      this.changeStateProperty({ key: "isShowPlayPage", value: false });
    },

    //喜欢歌曲
    addLike() {
      if (this.$cookies.get("uid") === null) {
        this.$toast("请先登录");
      } else {
        this.likeshow = true;
        let Like = {
          name: this.songName,
          singname: this.songerName,
          img: this.picUrl,
          id: this.audioId,
          singid: this.singerId,
          like: true,
        };

        this.addlikesong(Like);

        // 存放在本地储存中
        localStorage.setItem("Likesong", JSON.stringify(this.Likesong));
      }
    },

    //播放或者暂停
    playAndStop() {
      if (this.isPaused) {
        //播放音频
        this.audioEle.play();
        this.changeStateProperty({ key: "isPaused", value: false });
      } else {
        //暂停音频
        this.audioEle.pause();
        this.changeStateProperty({ key: "isPaused", value: true });
      }
    },

    ...mapMutations(["changeStateProperty", "addlikesong"]),

    //关闭播放主页
    closePlayPage() {
      this.changeStateProperty({ key: "isShowPlayPage", value: false });
    },

    // 点击播放音频
    playAudio(id, picUrl, songName, songerName, singerId) {
      if (id === this.audioId) {
        if (!this.audioEle.paused) {
          //暂停歌曲
          this.audioEle.pause();
          this.changeStateProperty({ key: "isPaused", value: true });
        } else {
          //播放歌曲
          this.audioEle.play();
          this.changeStateProperty({ key: "isPaused", value: false });
        }
        return;
      }

      //获取音频url
      this.axios({
        method: "get",
        url: "/song/url",
        params: {
          id,
        },
      })
        .then((res) => {
          let audioUrl = res.data.data[0].url;
          this.changeStateProperty({ key: "audioUrl", value: audioUrl });
          this.changeStateProperty({ key: "audioId", value: id });
          //获取暂停状态
          this.changeStateProperty({ key: "isPaused", value: false });
        })
        .catch((err) => {
          
        });

      //获取播放图片
      this.changeStateProperty({ key: "picUrl", value: picUrl });

      this.changeStateProperty({ key: "songName", value: songName });
      this.changeStateProperty({ key: "songerName", value: songerName });
      this.changeStateProperty({ key: "singerId", value: singerId });

      //获取专辑评论数据
      this.axios({
        method: "get",
        url: "/comment/music",
        params: {
          id,
        },
      })
        .then((res) => {
          this.changeStateProperty({
            key: "audioComment",
            value: res.data.hotComments,
          });
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
.play {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 99;
  background-color: rgba(0, 0, 0, 0.9);
  transform-origin: 100% 100%;
  transition: transform 0.3s linear;
  overflow: hidden;
}

.control-bg {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -99;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  filter: blur(6px);
}

//导航栏
.head {
  width: 100%;
  display: flex;
  padding: 4px 16px;
  box-sizing: border-box;
  align-items: center;
}

.left {
  margin-right: auto;
  font-size: 24px;
  color: #fff;
}

.right {
  margin-left: auto;
  font-size: 24px;
  color: #fff;
}

.center {
  width: 270px;
  margin: auto;
  display: flex;
  flex-wrap: wrap;
}

.song {
  width: 100%;
  height: 30px;
  font-size: 16px;
  color: #fff;
  font-weight: 600;
  padding-top: 6px;
  text-align: center;
}

.songer {
  width: 100%;
  display: flex;
  align-items: center;
  padding-top: 4px;
  color: #fff;
}

.songerbox {
  width: 100px;
  margin: auto;
  text-align: center;
}

.follow {
  width: 14px;
  height: 12px;
  line-height: 12px;
  text-align: center;
  border: 1px solid aliceblue;
  border-radius: 50%;
  margin-top: 2px;
  position: relative;
  right: 70px;
}

//转盘
.disc {
  width: 270px;
  height: 270px;
  border-radius: 50%;
  margin: auto;
  background: url("../assets/Blackglue.png") no-repeat center;
  background-size: cover;
  position: relative;
  top: 100px;
}

.disc img {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  position: relative;
  top: 60px;
  left: 60px;
}

// 歌词
.lyric {
  width: 90%;
  height: 240px;
  margin: auto;
  position: relative;
  top: 130px;
  display: flex;
  flex-wrap: wrap;
  overflow: hidden;
}

.lyricbox {
  width: 100%;
  position: relative;
  top: 0;
}

.item {
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  color: #9f9f9f;
  font-size: 16px;
  transition: top 0.15s linear;
  &.active {
    font-size: 20px;
    font-weight: 600;
    color: #fff;
  }
}

//功能
.function {
  width: 100%;
  position: absolute;
  bottom: 100px;
  left: 0;
  right: 0;
}

.functionlist {
  width: 76%;
  display: flex;
  margin: auto;
  justify-content: space-around;
}

.functionbox {
  width: 30px;
  height: 30px;
}

.functionbox img {
  width: 24px;
  height: 24px;
  position: relative;
  top: 2px;
  left: 3px;
}

//进度条
.bar {
  width: 100%;
  display: flex;
  padding: 20px 16px;
  box-sizing: border-box;
  align-items: center;
  position: absolute;
  bottom: 50px;
  left: 0;
  right: 0;
}

.timeleft {
  width: 50px;
  color: #fff;
}

.timeright {
  width: 50px;
  margin-left: 20px;
  padding-left: 10px;
  color: #fff;
}

.slider {
  width: 100%;
  height: 20px;
  &/deep/.van-slider {
    top: 8px;
    left: 18px;
  }
  &/deep/.van-slider__button {
    width: 10px;
    height: 10px;
  }
}

//播放
.playtion {
  width: 100%;
  position: absolute;
  bottom: 10px;
  left: 0;
  right: 0;
}

.playlist {
  width: 76%;
  display: flex;
  margin: auto;
  justify-content: space-around;
  align-items: center;
}

.playbox {
  width: 30px;
  height: 30px;
  &:nth-child(3) {
    width: 50px;
    height: 50px;
  }
  &:nth-child(3) img {
    width: 50px;
    height: 50px;
    position: relative;
    top: 0;
    left: 0;
  }
}

.playbox img {
  width: 24px;
  height: 24px;
  position: relative;
  top: 2px;
  left: 3px;
}

// 评论列表
.commenttitle {
  width: 100%;
  font-size: 16px;
  font-weight: 600;
  padding: 10px 20px;
  box-sizing: border-box;
  position: fixed;
  background-color: #fff;
  z-index: 99;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
}

.commentmain {
  margin-top: 40px;
}

.commentlist {
  width: 100%;
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 10px 14px;
  border-bottom: 1px solid #9f9f9f3a;
}

.listhead {
  width: 50px;
}

.listtext {
  width: 82%;
}

.listtextbox {
  display: flex;
  align-items: center;
}

.like {
  font-size: 20px;
  margin-left: auto;
  display: flex;
  align-items: center;
}

.liketxt {
  font-size: 12px;
  color: #9f9f9f;
}

.commenttext {
  width: 100%;
  padding-top: 6px;
  font-size: 14px;
}

//播放列表
.songlist {
  &/deep/.van-popup--center {
    top: 64%;
    width: 90%;
    height: 64%;
    padding: 10px 20px;
    background-color: #fff;
    box-sizing: border-box;
    border-radius: 14px;
  }
}

.playtit {
  width: 100%;
  height: 30px;
  font-size: 18px;
  font-weight: 600;
}

//歌曲列表
.tabbox {
  width: 100%;
  display: flex;
  align-items: center;
  margin-top: 10px;
  &/deep/.van-icon {
    left: 20px;
    bottom: 6px;
  }
}

.sealing {
  width: 40px;
  height: 40px;
}

.sealing {
  &/deep/ .van-image__img {
    width: 40px;
    height: 40px;
  }
}

.tabname {
  width: 58%;
  display: flex;
  flex-wrap: wrap;
  padding-left: 6px;
}

.tabsongname {
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  padding-left: 10px;
}

.tabtxt {
  display: flex;
  font-size: 12px;
  color: #9f9f9f;
  align-items: center;
}

.textbox {
  width: 30px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border: 1px solid red;
  color: red;
  transform: scale(0.5);
}

.tabplay {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 20px;
}

.share {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 10px;
}
</style>